package com.changwu.cwbase.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.Utils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.changwu.cwbase.R;

import me.wcy.htmltext.HtmlImageLoader;
import me.wcy.htmltext.HtmlText;

public class AppViewUtil {
    private static Context context = Utils.getApp();

    public static void show(ImageView img, String path) {
        if (StringUtils.isEmpty(path) || img == null) {
            return;
        }
        Glide.with(Utils.getApp()).load(path)
                .into(img);
    }

    public static void show(ImageView img, int path) {
        if (path == 0 || img == null) {
            return;
        }
        Glide.with(Utils.getApp()).load(path)
                .into(img);
    }

    public static void show(TextView tv, String content) {
        if (StringUtils.isEmpty(content) || tv == null) {
            return;
        }
        tv.setText(content);
    }


    public static void showHtml(final TextView tv, String content) {
        HtmlText.from(content)
                .setImageLoader(new HtmlImageLoader() {
                    @Override
                    public void loadImage(String url, final Callback callback) {
                        Glide.with(context)
                                .asBitmap()
                                .load(url)
                                .into(new SimpleTarget<Bitmap>() {
                                    @Override
                                    public void onResourceReady(Bitmap resource,
                                                                Transition<? super Bitmap> glideAnimation) {
                                        callback.onLoadComplete(resource);
                                    }

                                    @Override
                                    public void onLoadFailed(@Nullable Drawable errorDrawable) {
                                        callback.onLoadFailed();
                                    }
                                });
                    }

                    @Override
                    public Drawable getDefaultDrawable() {
                        return context.getResources().getDrawable(R.drawable.ic_empty);
                    }

                    @Override
                    public Drawable getErrorDrawable() {
                        return context.getResources().getDrawable(R.drawable.ic_empty);
                    }

                    @Override
                    public int getMaxWidth() {
                        return getTextWidth(tv);
                    }

                    @Override
                    public boolean fitWidth() {
                        return false;
                    }
                })
                .into(tv);
    }

    private static int getTextWidth(TextView tv) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        return dm.widthPixels - tv.getPaddingLeft() - tv.getPaddingRight();
    }

    public static void showTime(TextView tv, long time) {
        if (tv == null || time == 0) {
            return;
        }
        tv.setText(DateUtil.formatDate(time, DateUtil.DATE1));
    }
}
